using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BrightIdeasSoftware;

namespace HeaderWordWrapDemo
{
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public int Stock { get; set; }
        public bool IsActive { get; set; }
    }

    public partial class MainForm : Form
    {
        private VirtualObjectListView _olv;
        private List<Product> _products;

        public MainForm()
        {
            InitializeComponent();
            InitializeListView();
            LoadData();
        }

        private void InitializeListView()
        {
            _olv = new VirtualObjectListView
            {
                Dock = DockStyle.Fill,
                View = View.Details,
                GridLines = true,
                FullRowSelect = true,
                UseAlternatingBackColors = true,
                
                // 启用列标题自动换行（关键属性）
                HeaderWordWrap = true,
                
                // 可选：设置标题字体（影响换行效果）
                HeaderFont = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point)
            };

            // 添加列（包含长标题文本）
            _olv.Columns.AddRange(new OLVColumn[]
            {
                new OLVColumn("产品编号", "Id") { Width = 80 },
                new OLVColumn("产品名称", "Name") { Width = 150 },
                new OLVColumn("产品单价（元）", "Price") 
                { 
                    Width = 120,
                    AspectToStringConverter = val => $"{Convert.ToDecimal(val):C2}",
                    TextAlign = HorizontalAlignment.Right
                },
                new OLVColumn("库存数量（件）", "Stock") { Width = 120 },
                new OLVColumn("是否处于在售状态", "IsActive") 
                { 
                    Width = 150,
                    AspectToStringConverter = val => (bool)val ? "是" : "否"
                }
            });

            Controls.Add(_olv);
        }

        private void LoadData()
        {
            _products = new List<Product>
            {
                new Product { Id = 1, Name = "笔记本电脑", Price = 5999.99m, Stock = 20, IsActive = true },
                new Product { Id = 2, Name = "机械键盘", Price = 299.50m, Stock = 50, IsActive = true },
                new Product { Id = 3, Name = "无线鼠标", Price = 129.99m, Stock = 30, IsActive = true },
                new Product { Id = 4, Name = "有线耳机", Price = 89.99m, Stock = 15, IsActive = false }
            };

            _olv.SetObjects(_products);
        }

        private void InitializeComponent()
        {
            SuspendLayout();
            ClientSize = new Size(800, 450);
            Name = "MainForm";
            Text = "HeaderWordWrap 示例";
            ResumeLayout(false);
        }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
    }
}
